package com.fjec.market.mapper.sqlbuilder;

import java.util.List;

public class PurchaseSqlBuilder {
    public String getPurchasesByPurchaseIdsAndMarkCode(int markCode, List<Long> purchaseIds) {
        StringBuilder sb = new StringBuilder();
        if (markCode == 0) {
            sb.append("select * from info_purchase where CompHeadPic is not null ");
            if (purchaseIds.size() != 0) {
                sb.append(" and Id in (");
                for (Long id : purchaseIds) sb.append(id + ",");
                sb.deleteCharAt(sb.length() - 1);
                sb.append(" )");

            }
            sb.append(" order by rand() ");

        } else {
            sb.append("select purchase.* from info_purchase purchase, biz_market_busi bmb, info_market market where");
            sb.append(" CompHeadPic is not null and purchase.Id=bmb.BusiId and bmb.MarkCode=market.MarkCode and market.MarkCode=").append(markCode);

            if (purchaseIds.size() != 0) {
                sb.append(" and purchase.Id in (");
                for (Long id : purchaseIds) sb.append(id + ",");
                sb.deleteCharAt(sb.length() - 1);
                sb.append(")");
            }

            sb.append(" order by rand() ");
//            sb.append(" and purchase.Id >= (select ceil(rand()*(select MAX(Id) from info_purchase))) ");
        }

        return sb.toString();
    }
}
